<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/4/21 0021
 * Time: 18:03
 */

namespace app\home\model\report;
use app\home\model\BaseModel;
use think\Db;

class   DetectionOrder extends BaseModel
{
    public $result=[];
    public $detection=[];
    public $user=[];
//    protected $append=["execute_name","type_name"];
    /**设置用户信息
     * @param int $uId
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function setUser($u_id=0){
        if($u_id){
            $this->user=Db::table("dealer")->where(["u_id"=>$u_id])->find();
        }
        $this->result['u_id']       =$this->user["u_id"];
        $this->result['u_phone']    =$this->user["phone"];

        return $this;
    }
    /**设置商品信息
     * @param int $dId
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function setDetection($dId=0){
        if($dId){
            $this->detection  =   Db::table("detection")->field(['question','introduces'],true)->where(["d_id"=>$dId])->find();
        }
        $this->result['c_id']       =$this->detection["items_cate"];//分类id
        $this->result['d_id']       =$this->detection["d_id"];//测评商品id
        $this->result['title']                  =$this->detection["title"];//测评名称
        $this->result['o_detection_json']       =json_encode($this->detection);//备注信息
        $this->result['o_amount_price']         =$this->detection["price_preferential"];//商品金额
        $this->result['o_type']                 =($this->detection["is_pay"]===1)?1:2;//1=支付 2= 免费
        return $this;
    }
    /**设置订单信息
     * @param $o_type
     * @param $o_coupon_price
     * @param $o_pay
     * @return $this
     */
    public function setOrder($o_coupon_price=0,$o_pay=0){

        $this->result['o_number']            ="YD".getRandChar(12);//分类id
        $this->result['o_create_time']       =date("Y-m-d H:i:s",time());//分类id
        $this->result['o_coupon_price']      =$o_coupon_price;//优惠金额
        $pay_price                           =$this->result['o_amount_price'] -$this->result['o_coupon_price'];
        if($pay_price < 0){
            $pay_price =0;
        }

        $this->result['o_pay_price']         =$pay_price ;
            if($this->result['o_type'] == 2){       //如果是免费版的
                $o_pay = 1;
            }
        $this->result['o_pay']               =$o_pay;//0=未支付 1=已支付 2 = 退款 3 = 失效
        return $this;
    }

    /**获取订单号
     * @return mixed
     */
    public function getOrderNumber(){
        return $this->result['o_number'];
    }

    /**获取价格
     * @return mixed
     */
    public function  getPrice(){
        return $this->result['o_pay_price'];
    }
    /**设置支付
     * @param $oPay
     * @param $oPaySign
     * @return $this
     */
    public function setPay($oPay,$oPaySign){

        $this->result['o_pay']          =$oPay;//0=未支付 1=已支付 2 = 退款 3 = 失效
        $this->result['o_pay_sign']     =$oPaySign;//支付方式
//            $this->result['o_pay_number']   ='';
//            $this->result['o_pay_time']     =date("Y-m-d H:i:s",time());
        return $this;
    }
    /**修改订单
     * @param array $where
     * @return MoguDetectionOrder|bool
     */
    public function updateOrder($where=[]){
        if(empty($where)){
            return false;
        }
        $bool = $this->where($where)->update($this->result);
        return $bool;
    }

    /**
     * @return int|string
     */
    public function createOrder(){
        return  $this->insert($this->result);
    }

    /**创建订单
     * @param int $uId
     * @param int $dId
     * @param $o_type
     * @return bool|int|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public   function detectionOrder($uId=0,$dId=0){

        if(empty($this->user) && !$uId){
            return false;
        }
        if(empty($this->detection) && !$dId){
            return false;
        }

        return $this->setUser($uId)->setDetection($dId)->setOrder()->createOrder();
    }
}
